package dao

import (
	"fmt"
	"time"

	"cron-go/configs"
	"cron-go/internal/pkg/crontab/models"
	"cron-go/internal/pkg/database/mysql"

	"github.com/go-liam/util/conv"
	"github.com/go-liam/util/response"
)

var ReadyDaoServer = new(ReadyDao)

type ReadyDao struct {
}

func (e *ReadyDao) FindOneByName(name string) (*models.TaskReadyMysqlModel, error) {
	item := new(models.TaskReadyMysqlModel)
	sql := "select * from cron_ready_task where name =?  and is_delete =0 limit 1 "
	v := mysql.GetDB(mysql.DB_NAME).Raw(sql, name).Scan(item)
	return item, v.Error
}

func (e *ReadyDao) FindOneByID(id int64) (*models.TaskReadyMysqlModel, error) {
	item := new(models.TaskReadyMysqlModel)
	sql := "select * from cron_ready_task where id =?  and is_delete =0 limit 1 "
	v := mysql.GetDB(mysql.DB_NAME).Raw(sql, id).Scan(item)
	return item, v.Error
}

func (e *ReadyDao) Create(item *models.TaskReadyMysqlModel) (int64, error) {
	item.CreatedAt = time.Now().Unix()
	item.UpdatedAt = item.CreatedAt
	item.Date = conv.TimeNowFormat("2006-01-02")
	v := mysql.GetDB(mysql.DB_NAME).Create(item)
	return v.RowsAffected, v.Error
}

func (e *ReadyDao) FindMulti(p *response.Pagination, s *response.ListParameter) ([]*models.TaskReadyMysqlModel, error) {
	var result []*models.TaskReadyMysqlModel
	sqlLimit := fmt.Sprintf(" limit %d , %d  ", (p.Current-1)*p.PageSize, p.PageSize)
	sqlWhere := " is_delete = 0 " + s.WhereSt
	sql := "select * from cron_ready_task where " + sqlWhere + s.OrderSt + sqlLimit
	mysql.GetDB(mysql.DB_NAME).Model(&models.TaskReadyMysqlModel{}).Where(sqlWhere).Count(&p.Total)
	v := mysql.GetDB(mysql.DB_NAME).Raw(sql).Scan(&result)
	return result, v.Error
}

func (e *ReadyDao) FindAll(s *response.ListParameter) ([]*models.TaskReadyMysqlModel, error) {
	var result []*models.TaskReadyMysqlModel
	sqlWhere := " is_delete = 0 " + s.WhereSt
	sql := "select * from cron_ready_task where " + sqlWhere + s.OrderSt
	v := mysql.GetDB(mysql.DB_NAME).Raw(sql).Scan(&result)
	return result, v.Error
}

func (e *ReadyDao) Update(ids []int64, status int) (int64, error) {
	sql := fmt.Sprintf("update `cron_ready_task` set `status` = ? where `id` in (%v) ", conv.ArrayToString(ids, ","))
	v := mysql.GetDB(mysql.DB_NAME).Exec(sql, status)
	return v.RowsAffected, v.Error
}

func (e *ReadyDao) Delete(ids []int64) (int64, error) {
	sql := fmt.Sprintf("update `cron_ready_task` set `is_delete` = 1 where `id` in (%v) ", conv.ArrayToString(ids, ","))
	v := mysql.GetDB(mysql.DB_NAME).Exec(sql)
	return v.RowsAffected, v.Error
}

func (e *ReadyDao) Clear() (int64, error) {
	date := time.Now().Unix() - 1*60*60*24
	sql := fmt.Sprintf(" delete from `cron_ready_task` where `id` > 0 and `is_delete` = 1 and `created_at` < %v ; ", date)
	println(sql)
	v := mysql.GetDB(mysql.DB_NAME).Exec(sql)
	return v.RowsAffected, v.Error
}

func (e *ReadyDao) CreateByTaskModel(t *models.TaskMysqlModel) (int64, error) {
	item := new(models.TaskReadyMysqlModel)
	item.TaskID = t.ID
	item.ServerName = configs.HOST
	item.WorkerStatus = 0
	return e.Create(item)
}
